package com.fxcalibre.fxml.control;

import java.util.Iterator;
import java.util.List;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

import com.fxcalibre.db.beans.Person;
import com.fxcalibre.fxml.control.events.DataChangedEvent;
import com.fxcalibre.fxml.view.FXMLViewLoader;
import com.fxcalibre.hibernate.GenericCriteria;
import com.fxcalibre.hibernate.dao.MediaItemsDAO;
import com.fxcalibre.hibernate.dao.PeopleDAO;
import com.fxcalibre.icons.ImageLoader;
import com.fxcalibre.utils.Constants;

public class TreeFilter extends TreeView<Object>
{
	ObservableList<GenericCriteria> filters = FXCollections.observableArrayList();

	public TreeFilter()
	{
		FXMLViewLoader.load(this, "TreeFilter.fxml");
		reload();
	}

	public void reload()
	{
		TreeItem<Object> rootNode = new TreeItem<Object>();
		rootNode.setExpanded(true);

		TreeItem<Object> typesNode = new TreeItem<Object>("Tipo");
		typesNode.setExpanded(true);

		for (int i = 0; i < Constants.mediaTypes.length; i++)
		{
			TreeItem<Object> typeNode = new TreeItem<Object>(Constants.mediaTypes[i]);
			typesNode.getChildren().add(typeNode);
		}

		rootNode.getChildren().add(typesNode);

		TreeItem<Object> directoresNode = new TreeItem<Object>("Directores", ImageLoader.getImageView("user16.png"));
		directoresNode.setExpanded(true);

		List<Person> directores = PeopleDAO.getDirectors();

		for (Iterator<Person> iterator = directores.iterator(); iterator.hasNext();)
		{
			TreeItem<Object> directorNode = new TreeItem<Object>(iterator.next(), ImageLoader.getImageView("loan.png"));
			directoresNode.getChildren().add(directorNode);
		}

		rootNode.getChildren().add(directoresNode);

		TreeItem<Object> actoresNode = new TreeItem<Object>("Actores", ImageLoader.getImageView("user16.png"));
		actoresNode.setExpanded(false);

		List<Person> actors = PeopleDAO.getActors();

		for (Iterator<Person> iterator = actors.iterator(); iterator.hasNext();)
		{
			Person person = iterator.next();
			TreeItem<Object> actorNode = new TreeItem<Object>(person, ImageLoader.getImageView("loan.png"));
			actoresNode.getChildren().add(actorNode);
		}

		rootNode.getChildren().add(actoresNode);
		setRoot(rootNode);
		getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<Object>>()
		{

			@Override
			public void changed(ObservableValue<? extends TreeItem<Object>> observable, TreeItem<Object> oldValue, TreeItem<Object> newValue)
			{
				if (newValue.isLeaf())
				{
					filters.clear();
					GenericCriteria gCriterion = MediaItemsDAO.crearCriterio();

					if ("Directores".equals(newValue.getParent().getValue()))
					{
						gCriterion.addSubcriteria("crew", "pk.role", GenericCriteria.OP_IGUAL, "Director");
						gCriterion.addSubcriteria("crew", "pk.person", GenericCriteria.OP_IGUAL, ((Person) newValue.getValue()));

					} else if ("Actores".equals(newValue.getParent().getValue()))
					{
						gCriterion.addSubcriteria("crew", "pk.role", GenericCriteria.OP_IGUAL, "Actor");
						gCriterion.addSubcriteria("crew", "pk.person", GenericCriteria.OP_IGUAL, ((Person) newValue.getValue()));
					}else if ("Tipo".equals(newValue.getParent().getValue()))
					{
						gCriterion.addCriterion("type", GenericCriteria.OP_IGUAL, newValue.getValue());
					}

					filters.add(gCriterion);
					System.out.println("Add filter");
				} else
				{
					filters.clear();
				}

				fireEvent(new DataChangedEvent());
			}

		});
	}

	public void setFilters(ObservableList<GenericCriteria> filters)
	{
		this.filters = filters;
	}
}
